
package code.codeworld.info.service.impl;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import code.codeworld.common.kits.Result;
import code.codeworld.info.mapper.InfoJPA;
import code.codeworld.info.mapper.InfoMapper;
import code.codeworld.info.model.Info;
import code.codeworld.info.model.vo.InfoVo;
import code.codeworld.info.service.InfoService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * InfoService接口实现类
 *
 * @author freecode
 * @date 2023/10/26 00:20
 **/
@Service
public class InfoServiceImpl extends ServiceImpl<InfoMapper, Info> implements InfoService {
    @Resource
    private InfoJPA infoJPA;


    @Override
    public Page<Info> page(InfoVo InfoVo) {
        LambdaQueryWrapper<Info> query = new LambdaQueryWrapper<Info>();
        query.eq(Info::getType, InfoVo.getType());
        query.eq(Objects.nonNull(InfoVo.getSubtype()), Info::getSubtype, InfoVo.getSubtype());
        query.between(Info::getCreateTime, InfoVo.getCreateTime(), DateUtils.addDays(InfoVo.getCreateTime(), 1));
        return super.page(new Page<>(InfoVo.getPage(), InfoVo.getLimit()), query);
    }

    @Override
    public List<Info> list(InfoVo InfoVo) {
        LambdaQueryWrapper<Info> query = new LambdaQueryWrapper<Info>();
        query.eq(Info::getType, InfoVo.getType());
        query.eq(Objects.nonNull(InfoVo.getSubtype()), Info::getSubtype, InfoVo.getSubtype());
        if (!InfoVo.getType().equals("信息")) {
            query.between(Info::getUpdateTime, DateUtil.beginOfDay(InfoVo.getUpdateTime()), DateUtil.endOfDay(InfoVo.getUpdateTime()));
        }
        return super.list(query);
    }

    @Override
    public Info detail(Info Info) {
        return this.getOneById(Info.getId());
    }


    @Override
    public Result<String> editMark(Info info) {
        Info infoDb = this.getOneById(info.getId());
        if (Objects.isNull(infoDb)) {
            return Result.exception();
        }
        if (info.getStarNum() != null) {
            infoDb.setStarNum(infoDb.getStarNum() + info.getStarNum());
        }
        if (info.getCollectNum() != null) {
            infoDb.setCollectNum(infoDb.getCollectNum() + info.getCollectNum());
        }
        if (!StringUtils.isEmpty(info.getComment())) {
            infoDb.setComment(infoDb.getComment() + "\\\\" + info.getComment());
        }
        try {
            infoJPA.save(info);
            return Result.ok();

        } catch (Exception e) {
            e.printStackTrace();
            return Result.exception(e.getMessage());
        }
    }


    public Info getOneById(Integer id) {
        return infoJPA.getReferenceById(id);
    }

    public List<Info> getAllById(List<Info> infoList) {
        Iterable<Integer> ids = infoList.stream().map(Info::getId).collect(Collectors.toList());
        return infoJPA.findAllById(ids);
    }

}
